package io.yincs.test.test1;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    private Button mTrueBtn;
    private Button mFalseBtn;

    private Button mNextButton;
    private Button mCheatButton;

    private TextView mQuestionView;

    private int mCurrentQuestionIndex = 0;

    private QuestionTrueFalse[] mQuestionBank = new QuestionTrueFalse[]{
            new QuestionTrueFalse(R.string.question1, true),
            new QuestionTrueFalse(R.string.question2, false),
            new QuestionTrueFalse(R.string.question3, true),
            new QuestionTrueFalse(R.string.question4, true),
            new QuestionTrueFalse(R.string.question5, false),
            new QuestionTrueFalse(R.string.question6, false),
            new QuestionTrueFalse(R.string.question7, true),
            new QuestionTrueFalse(R.string.question8, false),
            new QuestionTrueFalse(R.string.question9, false),
            new QuestionTrueFalse(R.string.question10, true)
    };

    private void updateQuestionToTextView() {
        int question = mQuestionBank[mCurrentQuestionIndex].getQuestion();
        mQuestionView.setText(question);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_main);

        mQuestionView = (TextView) findViewById(R.id.question_text_view);

        mTrueBtn = (Button) findViewById(R.id.true_btn);
        mTrueBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkAnswer(true);
            }
        });

        mFalseBtn = (Button) findViewById(R.id.false_btn);
        mFalseBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkAnswer(false);
            }
        });

        mNextButton = (Button) findViewById(R.id.next_btn);
        mNextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCurrentQuestionIndex = (mCurrentQuestionIndex + 1) % mQuestionBank.length;
                updateQuestionToTextView();
            }
        });

        mCheatButton = (Button) findViewById(R.id.cheat_btn);
        mCheatButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });


        updateQuestionToTextView();


    }

    private void checkAnswer(boolean userPressedTrue) {

        boolean answerIsTrue = mQuestionBank[mCurrentQuestionIndex].isTrueQuestion();

        int messageResId = 0;

        if ( userPressedTrue == answerIsTrue ) {
            messageResId = R.string.correct_toast;
        } else {
            messageResId = R.string.incorrect_toast;
        }

        Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show();

    }

}
